﻿using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
using StockholmsStad.Web.UI.Class.Controller;
using StockholmsStad.Web.UI.Class.Interfaces;
using StockholmsStad.Web.UI.Class.Model;
using StockholmsStad.Web.UI.Class.Utilities;
using StockholmsStad.Web.UI.UIServiceReference;

namespace StockholmsStad.Web.UI.Test
{
    /// <summary>
    ///This is a test class for RegistrationControllerTest and is intended
    ///to contain all RegistrationControllerTest Unit Tests
    ///</summary>
    [TestClass]
    public class RegistrationControllerTest
    {
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        #region Additional test attributes

        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //

        #endregion

        /// <summary>
        ///A test for SubmitRegistrationSummary
        ///</summary>
        [TestMethod]
        public void SubmitRegistrationSummaryTest()
        {
            var mocks = new MockRepository();
            var contactView = mocks.StrictMock<IRegistration2_Contact>();
            var summaryView = mocks.StrictMock<IRegistration3_Summary>();
            var finnishedView = mocks.StrictMock<IRegistration4_Finnished>();
            var model = mocks.DynamicMock<IRegistrationModel>();
            SetupResult.For(model.ContactEmail).Return("abc@cd.cd");
            var target = new RegistrationController(model, contactView, summaryView, finnishedView);
            Expect.Call(() => finnishedView.ContactEmail = "abc@cd.cd");
            mocks.ReplayAll();
            target.SubmitRegistrationSummary();
            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for SubmitRegistrationPersons
        ///</summary>
        [TestMethod]
        public void SubmitRegistrationPersonsTest()
        {
            var mocks = new MockRepository();
            var contactView = mocks.DynamicMock<IRegistration2_Contact>();
            var summaryView = mocks.StrictMock<IRegistration3_Summary>();
            var finnishedView = mocks.StrictMock<IRegistration4_Finnished>();
            var session = new DictionarySessionAdapter(new Dictionary<string, object>());
            var model = new RegistrationModel(session, mocks.StrictMock<IServiceFacade>());
            var person1 = new Person("Svenne", "Snorslev", null, true, "", true, null);
            var person2 = new Person("Svenne2", "Snorslev2", null, true, "", true, null);
            var target = new RegistrationController(model, contactView, summaryView, finnishedView);
            Expect.Call(() => contactView.UpdateContactPersonListName(person1.FirstNames + " " + person1.LastName));
            mocks.ReplayAll();
            target.SubmitRegistrationPersons(person1, person2);
            Assert.AreEqual(model.Couple.Person1, person1);
            Assert.AreEqual(model.Couple.Person2, person2);
            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for SubmitRegistrationContact
        ///</summary>
        [TestMethod]
        public void SubmitRegistrationContactTest()
        {
            var mocks = new MockRepository();
            var contactView = mocks.StrictMock<IRegistration2_Contact>();
            var summaryView = mocks.StrictMock<IRegistration3_Summary>();
            var finnishedView = mocks.StrictMock<IRegistration4_Finnished>();
            var session = new DictionarySessionAdapter(new Dictionary<string, object>());
            var model = new RegistrationModel(session, mocks.StrictMock<IServiceFacade>());
            var person1 = new Person("Svenne", "Snorslev", null, true, "", true, null);
            var person2 = new Person("Svenne2", "Snorslev2", null, true, "", true, null);
            var cerInfo = new Ceremony
                              {
                                  CeremonyLanguage = "Svenska",
                                  CertificateLanguage = "Svenska",
                                  SpecialNeeds = new string[0],
                              };
            var contact = new ContactPerson
                              {
                                  FirstNames = "",
                                  LastName = "",
                                  Telephone = "",
                                  Email = "abc@cd.cd",
                              };
            model.Couple.Person1 = person1;
            model.Couple.Person2 = person2;
            var target = new RegistrationController(model, contactView, summaryView, finnishedView);
            Expect.Call(() => summaryView.Person1 = person1);
            Expect.Call(() => summaryView.Person2 = person2);
            Expect.Call(() => summaryView.Contact = contact);
            Expect.Call(() => summaryView.Ceremony = cerInfo);
            mocks.ReplayAll();
            target.SubmitRegistrationContact(contact, cerInfo, false);
            Assert.AreEqual(model.Couple.ContactPerson, contact);
            Assert.AreEqual(model.Ceremony, cerInfo);
            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for SubmitRegistrationContact
        ///</summary>
        [TestMethod]
        public void SubmitRegistrationContactTest2()
        {
            var mocks = new MockRepository();
            var contactView = mocks.StrictMock<IRegistration2_Contact>();
            var summaryView = mocks.StrictMock<IRegistration3_Summary>();
            var finnishedView = mocks.StrictMock<IRegistration4_Finnished>();
            var session = new DictionarySessionAdapter(new Dictionary<string, object>());
            var model = new RegistrationModel(session, mocks.StrictMock<IServiceFacade>());
            var person1 = new Person("Svenne", "Snorslev", null, true, "", true, null);
            var person2 = new Person("Svenne2", "Snorslev2", null, true, "", true, null);
            var cerInfo = new Ceremony
                              {
                                  CeremonyLanguage = "Svenska",
                                  CertificateLanguage = "Svenska",
                                  SpecialNeeds = new string[0],
                              };
            var contact = new ContactPerson
                              {
                                  FirstNames = "",
                                  LastName = "",
                                  Telephone = "12345",
                                  Email = "abc@cd.cd",
                              };
            model.Couple.Person1 = person1;
            model.Couple.Person2 = person2;
            var target = new RegistrationController(model, contactView, summaryView, finnishedView);
            Expect.Call(() => summaryView.Person1 = person1);
            Expect.Call(() => summaryView.Person2 = person2);
            Expect.Call(() => summaryView.Contact = contact);
            Expect.Call(() => summaryView.Ceremony = cerInfo);
            mocks.ReplayAll();
            target.SubmitRegistrationContact(contact, cerInfo, true);
            Assert.AreEqual(null, model.Couple.ContactPerson);
            Assert.AreEqual(cerInfo, model.Ceremony);
            Assert.AreEqual(contact.Email, model.Couple.Person1.Email);
            Assert.AreEqual(contact.Telephone, model.Couple.Person1.Phonenumber);
            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for RegistrationController Constructor
        ///</summary>
        [TestMethod]
        public void RegistrationControllerConstructorTest()
        {
            var mocks = new MockRepository();
            var contactView = mocks.StrictMock<IRegistration2_Contact>();
            var summaryView = mocks.StrictMock<IRegistration3_Summary>();
            var finnishedView = mocks.StrictMock<IRegistration4_Finnished>();
            var session = new DictionarySessionAdapter(new Dictionary<string, object>());
            var model = new RegistrationModel(session, mocks.StrictMock<IServiceFacade>());
            mocks.ReplayAll();
            var target = new RegistrationController(model, contactView, summaryView, finnishedView);
            Assert.IsNotNull(target);
            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for UpdateContactName
        ///</summary>
        [TestMethod()]
        public void UpdateContactNameTest()
        {
            var mocks = new MockRepository();
            var contactView = mocks.StrictMock<IRegistration2_Contact>();
            var summaryView = mocks.StrictMock<IRegistration3_Summary>();
            var finnishedView = mocks.StrictMock<IRegistration4_Finnished>();
            var session = new DictionarySessionAdapter(new Dictionary<string, object>());
            var model = new RegistrationModel(session, mocks.StrictMock<IServiceFacade>());
            model.Couple.Person1 = new Person("Svenne", "Snorslev", null, true, "", true, null);
            var target = new RegistrationController(model, contactView, summaryView, finnishedView);
            Expect.Call(contactView.ContactPersonListIsFirstPerson()).Return(true);
            Expect.Call(() => contactView.UpdateContactPersonName("Svenne", "Snorslev"));
            Expect.Call(() => contactView.UpdateContactPersonName(false));
            mocks.ReplayAll();
            target.UpdateContactName();
            mocks.VerifyAll();
        }
    }
}